package com.xiaozhi.service.impl;

import com.xiaozhi.VO.CourseVo;
import com.xiaozhi.entity.Course;
import com.xiaozhi.entity.Discussion;
import com.xiaozhi.mapper.CourseMapper;
import com.xiaozhi.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaozhi.utils.CommonUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 烟尘
 * @since 2023-03-15
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    public final static Integer MAX_SIZE=5;
    public final static Integer MAX_RANDOM=37;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public List<Course> getAllByRecommend() {
        List<Course> allByRecommend = courseMapper.getAllMes();
        Random random=new Random();
        HashSet<Integer> set=new HashSet<>();
        while (set.size()<MAX_SIZE){
            set.add(random.nextInt(MAX_RANDOM));
        }
        Iterator<Integer> iterator = set.iterator();
        List<Course> results=new ArrayList<>();
        while (iterator.hasNext()){
            results.add(allByRecommend.get(iterator.next()));
        }
        return results;
    }

    @Override
    public List<CourseVo<Course>> getFiveMes() {
        List<Course> allFive = courseMapper.getAllFive();
        return CommonUtil.addData(allFive);
    }

    @Override
    public  List<CourseVo<Course>> getAllMes() {
        List<Course> allMes = courseMapper.getAllMes();
        return CommonUtil.addData(allMes);
    }

    @Override
    public Course getCouAndRep(String id) {
        return courseMapper.getCouAndRep(id);
    }

}
